package cn.ainit.sys.query;

import java.util.List;

import javax.activation.DataSource;
import javax.persistence.Id;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import cn.ainit.sys.common.dao.Pagination;
import cn.ainit.sys.common.dao.SysBaseDao;
import cn.ainit.sys.constant.SystemConstant;
import cn.ainit.sys.ds.store.sysDataStore;
import cn.ainit.sys.query.datastore.DataStore;
import cn.ainit.sys.query.entity.QueryButton;
import cn.ainit.sys.query.entity.QueryColumn;
import cn.ainit.sys.ui.forms.Columns;

@Repository("dynamicQueryDaoImpl")
public class DynamicQueryDaoImpl extends SysBaseDao implements DynamicQueryDao{
	
//	@Override
//	public void getAllQueryColumns(String flag) throws Exception {
//		Pagination pagination = new Pagination(dbType, "select * from t_user ",1,1,jdbcTemplate);
//		System.out.println(pagination.getLastIndex());
//		System.out.println(pagination.getResultList().size());
//	}
	@Override
	public List<QueryButton> getQueryButtons(String partOrAll,String id) throws Exception {
		String hql = "";
		if (partOrAll != null && partOrAll.equalsIgnoreCase(SystemConstant.QUERY_SECOND) ) {
			hql = " from QueryButton qb where qb.secondPageFlag = 'T' and qb.dsid=?";
		}else{
			hql = " from QueryButton qb where qb.firstPageFlag = 'T' and qb.dsid=?";
		}
		Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
		query.setString(0, id);
		List<QueryButton> list = null;
		list = query.list();
		return list;
	}
	
	@Override
	public List<QueryColumn> getQueryColumns(String partOrAll,String id) throws Exception {
		String hql = "";
		if (partOrAll != null && partOrAll.equalsIgnoreCase(SystemConstant.QUERY_SECOND) ) {
			hql = " from QueryColumn qc where qc.secondPageFlag = 'T' and qc.dsid=?";
		}else{
			hql = " from QueryColumn qc where qc.firstPageFlag = 'T' and qc.dsid=?";
		}
		Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
		query.setString(0, id);
		List<QueryColumn> list = null;
		list = query.list();
		return list;
	}
	
	@Override
	public List<QueryColumn> getQueryShowColumns(String flag,String id) throws Exception {
		String hql = "";
		List<QueryColumn> listShow = null;
		try {
			hql = " from QueryColumn qc where qc.show = 'T' and qc.dsid=? order by qc.orderNum";
			Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
			query.setString(0, id);
			listShow = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listShow;
	}

	@Override
	public boolean adddataStore(sysDataStore dataStore) throws Exception {
		try {
			this.getSessionFactory().getCurrentSession().save(dataStore);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	@Override
	public List<Columns> getQueryUIColumns(String flag,String formsId) throws Exception {
		String hql = "";
		List<Columns> listShow = null;
		try {
			hql = " from Columns qc where qc.firstPageFlag = 'T' and qc.dsid =?";
			Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
			query.setString(0, formsId);
			listShow = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listShow;
	}

	@Override
	public DataStore querySql(String id) throws Exception {
		try {
			String hql = "from DataStore where dsId =?";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, id);
			List<Object> objects = query.list();
			DataStore dataStore = new DataStore();
			if (objects != null && objects.size() > 0 ) {
				dataStore =  (DataStore) objects.get(0);
			}
			return dataStore;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public List getRows(String sql, int pageNumber, int pageSize) throws Exception {
		List<Object> list = null;
		try {
			Pagination pagination = new Pagination(dbType, sql, pageNumber, pageSize, this.getJdbcTemplate());
			list = pagination.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
